home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FishMarket 1.0
/
FishMarket v1.0.iso
/
fishies
/
151-175
/
disk_166
/
stevie
/
source
/
normal.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-05-06
|
34KB
|
1,738 lines
/*
* STEVIE - Simply Try this Editor for VI Enthusiasts
*
* Code Contributions By : Tim Thompson twitch!tjt
* Tony Andrews onecom!wldrdg!tony
* G. R. (Fred) Walter watmath!watcgl!grwalter
*/
/*
* This file contains the main routine for processing characters in command
* mode as well as routines for handling the operators.
*/
#include "stevie.h"
static void
doshift(), dodelete(), doput(), dochange();
static void
startinsert();
static bool_t
dojoin();
static bool_t
doyank();
/*
* Macro evaluates true if char 'c' is a valid identifier character
*/
#define IDCHAR(c) (isalpha(c) || isdigit(c) || (c) == '_')
/*
* Operators
*/
#define NOP 0 /* no pending operation */
#define DELETE 1
#define YANK 2
#define CHANGE 3
#define LSHIFT 4
#define RSHIFT 5
#define CLEAROP (operator = NOP)/* clear any pending operator */
static int operator = NOP; /* current pending operator */
/*
* When a cursor motion command is made, it is marked as being a character or
* line oriented motion. Then, if an operator is in effect, the operation
* becomes character or line oriented accordingly.
*
* Character motions are marked as being inclusive or not. Most char. motions
* are inclusive, but some (e.g. 'w') are not.
*
* Generally speaking, every command in normal() should either clear any pending
* operator (with CLEAROP), or set the motion type variable.
*/
/*
* Motion types
*/
#define MBAD (-1) /* 'bad' motion type marks unusable yank buf */
#define MCHAR 0
#define MLINE 1
static int mtype; /* type of the current cursor motion */
static bool_t mincl; /* true if char motion is inclusive */
static int ybtype = MBAD;
static int ybcrossline = FALSE;
static LPTR startop; /* cursor pos. at start of operator */
/*
* Operators can have counts either before the operator, or between the
* operator and the following cursor motion as in:
*
* d3w or 3dw
*
* If a count is given before the operator, it is saved in opnum. If normal() is
* called with a pending operator, the count in opnum (if present) overrides
* any count that came later.
*/
static int opnum = 0;
#define DEFAULT1(x) (((x) == 0) ? 1 : (x))
/*
* normal
*
* Execute a command in normal mode.
*/
void
normal(c)
char c;
{
char *p;
int n;
int nn;
bool_t flag = FALSE;
int type = 0; /* used in some operations to modify type */
int dir = FORWARD; /* search direction */
char nchar = NUL;
bool_t finish_op;
LPTR temp_Curschar;
last_command = NUL;
/*
* If there is an operator pending, then the command we take this time
* will terminate it. Finish_op tells us to finish the operation before
* returning this time (unless the operation was cancelled).
*/
finish_op = (operator != NOP);
/*
* If we're in the middle of an operator AND we had a count before the
* operator, then that count overrides the current value of Prenum. What
* this means effectively, is that commands like "3dw" get turned into
* "d3w" which makes things fall into place pretty neatly.
*/
if (finish_op) {
if (opnum != 0)
Prenum = opnum;
} else
opnum = 0;
switch (c) {
case K_HELP:
CLEAROP;
if (help()) {
screenclear();
updateNextscreen();
}
break;
case CTRL('L'):
CLEAROP;
screenclear();
updateNextscreen();
break;
case CTRL('D'):
CLEAROP;
if (Prenum)
P(P_SS) = (Prenum > Rows - 1) ? Rows - 1 : Prenum;
scrollup((P(P_SS) < Rows) ? P(P_SS) : Rows - 1);
onedown((P(P_SS) < Rows) ? P(P_SS) : Rows - 1);
updateNextscreen();
break;
case CTRL('U'):
CLEAROP;
if (Prenum)
P(P_SS) = (Prenum > Rows - 1) ? Rows - 1 : Prenum;
scrolldown((P(P_SS) < Rows) ? P(P_SS) : Rows - 1);
oneup((P(P_SS) < Rows) ? P(P_SS) : Rows - 1);
updateNextscreen();
break;
case CTRL('F'):
CLEAROP;
if (nextline(Topchar) == NULL) {
beep();
break;
}
screenclear();
Prenum = DEFAULT1(Prenum);
while (Prenum > 0) {
*Curschar = *prevline(Botchar);
*Topchar = *Curschar;
Topchar->index = 0;
updateNextscreen();
Prenum--;
}
beginline(TRUE);
break;
case CTRL('B'):
CLEAROP;
if (prevline(Topchar) == NULL) {
beep();
break;
}
screenclear();
Prenum = DEFAULT1(Prenum);
while (Prenum > 0) {
*Curschar = *Topchar;
n = Rows - 1;
{
LPTR *lp = Curschar;
int l = 0;
while ((l < n) && (lp != NULL)) {
l += plines(lp);
*Topchar = *lp;
lp = prevline(lp);
}
}
Topchar->index = 0;
Prenum--;
}
beginline(TRUE);
updateNextscreen();
break;
case CTRL('E'):
CLEAROP;
scrollup(DEFAULT1(Prenum));
updateNextscreen();
break;
case CTRL('Y'):
CLEAROP;
scrolldown(DEFAULT1(Prenum));
updateNextscreen();
break;
case 'z':
CLEAROP;
switch (vgetc()) {
case NL: /* put Curschar at top of screen */
case CR:
*Topchar = *Curschar;
Topchar->index = 0;
updateNextscreen();
break;
case '.': /* put Curschar in middle of screen */
n = Rows / 2;
goto dozcmd;
case '-': /* put Curschar at bottom of screen */
n = Rows - 1;
/* FALLTHROUGH */
dozcmd:
{
LPTR *lp = Curschar;
int l = 0;
while ((l < n) && (lp != NULL)) {
l += plines(lp);
*Topchar = *lp;
lp = prevline(lp);
}
}
Topchar->index = 0;
updateNextscreen();
break;
default:
beep();
}
break;
case CTRL('G'):
CLEAROP;
fileinfo();
break;
case 'G':
mtype = MLINE;
*Curschar = *gotoline(Prenum);
beginline(TRUE);
break;
case 'H':
mtype = MLINE;
*Curschar = *Topchar;
for (n = Prenum; n && onedown(1); n--);
beginline(TRUE);
break;
case 'M':
mtype = MLINE;
*Curschar = *Topchar;
for (n = 0; n < Rows / 2 && onedown(1); n++);
beginline(TRUE);
break;
case 'L':
mtype = MLINE;
*Curschar = *prevline(Botchar);
for (n = Prenum; n && oneup(1); n--);
beginline(TRUE);
break;
case 'l':
case K_RARROW:
case ' ':
mtype = MCHAR;
mincl = FALSE;
n = DEFAULT1(Prenum);
while (n--) {
if (!oneright()) {
if (operator != DELETE && operator != CHANGE) {
beep();
} else {
if (lineempty(Curschar)) {
CLEAROP;
beep();
} else {
mincl = TRUE;
}
}
break;
}
}
set_want_col = TRUE;
break;
case 'h':
case K_LARROW:
case CTRL('H'):
mtype = MCHAR;
mincl = FALSE;
Prenum = DEFAULT1(Prenum);
n = Prenum;
while (n--) {
if (!oneleft()) {
if (operator != DELETE && operator != CHANGE) {
beep();
} else if (Prenum == 1) {
CLEAROP;
beep();
}
break;
}
}
set_want_col = TRUE;
break;
case '-':
flag = TRUE;
/* FALLTHROUGH */
case 'k':
case K_UARROW:
case CTRL('P'):
mtype = MLINE;
if (!oneup(DEFAULT1(Prenum))) {
CLEAROP;
beep();
} else if (flag)
beginline(TRUE);
break;
case '+':
case CR:
case NL:
flag = TRUE;
/* FALLTHROUGH */
case 'j':
case K_DARROW:
case CTRL('N'):
mtype = MLINE;
if (!onedown(DEFAULT1(Prenum))) {
CLEAROP;
beep();
} else if (flag)
beginline(TRUE);
break;
/*
* This is a strange motion command that helps make operators more
* logical. It is actually implemented, but not documented in the
* real 'vi'. This motion command actually refers to "the current
* line". Commands like "dd" and "yy" are really an alternate form of
* "d_" and "y_". It does accept a count, so "d3_" works to delete 3
* lines.
*/
case '_':
lineop:
mtype = MLINE;
if (!onedown(DEFAULT1(Prenum) - 1)) {
CLEAROP;
beep();
} else
beginline(TRUE);
break;
case '|':
mtype = MCHAR;
mincl = TRUE;
beginline(FALSE);
if (Prenum > 0)
*Curschar = *coladvance(Curschar, Prenum - 1);
Curswant = Prenum - 1;
break;
case CTRL(']'): /* :ta to current identifier */
CLEAROP;
{
char ch;
LPTR save;
save = *Curschar;
/*
* First back up to start of identifier. This doesn't match the
* real vi but I like it a little better and it shouldn't bother
* anyone.
*/
ch = gchar(Curschar);
while (IDCHAR(ch)) {
if (!oneleft())
break;
ch = gchar(Curschar);
}
if (!IDCHAR(ch))
oneright();
stuffReadbuff(":ta ");
/*
* Now grab the chars in the identifier
*/
ch = gchar(Curschar);
while (IDCHAR(ch)) {
stuffReadbuff(mkstr(ch));
if (!oneright())
break;
ch = gchar(Curschar);
}
stuffReadbuff("\n");
*Curschar = save; /* restore, in case of error */
}
break;
case '%':
mtype = MCHAR;
mincl = TRUE;
{
LPTR *pos;
if ((pos = showmatch()) == NULL) {
CLEAROP;
beep();
} else {
setpcmark();
*Curschar = *pos;
set_want_col = TRUE;
}
}
break;
/*
* Word Motions
*/
case 'B':
type = 1;
/* FALLTHROUGH */
case 'b':
mtype = MCHAR;
mincl = FALSE;
set_want_col = TRUE;
for (n = DEFAULT1(Prenum); n > 0; n--) {
LPTR *pos;
if ((Curschar->linep->prev == NULL) && (Curschar->index == 0)) {
CLEAROP;
beep();
break;
}
pos = bck_word(Curschar, type);
if (pos == NULL)
*Curschar = *gotoline(1); /* goto top of file */
else
*Curschar = *pos;
}
break;
case 'W':
type = 1;
/* FALLTHROUGH */
case 'w':
/*
* This is a little strange. To match what the real vi does, we
* effectively map 'cw' to 'ce', and 'cW' to 'cE'. This seems
* impolite at first, but it's really more what we mean when we say
* 'cw'.
*/
if (operator == CHANGE)
goto doecmd;
mtype = MCHAR;
mincl = FALSE;
set_want_col = TRUE;
for (n = DEFAULT1(Prenum); n > 0; n--) {
LPTR *pos;
if ((pos = fwd_word(Curschar, type)) == NULL) {
CLEAROP;
beep();
break;
} else
*Curschar = *pos;
}
break;
case 'E':
type = 1;
/* FALLTHROUGH */
case 'e':
doecmd:
mtype = MCHAR;
mincl = TRUE;
set_want_col = TRUE;
for (n = DEFAULT1(Prenum); n > 0; n--) {
LPTR *pos;
if ((pos = end_word(Curschar, type)) == NULL) {
CLEAROP;
beep();
break;
} else
*Curschar = *pos;
}
break;
case '$':
mtype = MCHAR;
mincl = TRUE;
while (oneright());
Curswant = 999; /* so we stay at the end */
break;
case '^':
flag = TRUE;
/* FALLTHROUGH */
case '0':
mtype = MCHAR;
mincl = TRUE;
beginline(flag);
break;
case 'A':
set_want_col = TRUE;
while (oneright());
ResetBuffers();
AppendToRedobuff("A");
goto doAPPENDcmd;
case 'a':
ResetBuffers();
AppendToRedobuff("a");
doAPPENDcmd:
CLEAROP;
/* Works just like an 'i'nsert on the next character. */
n = RowNumber(Curschar);
AppendPositionToUndoUndobuff(Curschar->index, n);
AppendToUndoUndobuff("a");
if (!lineempty(Curschar))
inc(Curschar);
n = RowNumber(Curschar);
AppendPositionToUndobuff(Curschar->index, n);
startinsert(FALSE);
CHANGED;
break;
case 'I':
beginline(TRUE);
ResetBuffers();
AppendToRedobuff("I");
goto doINSERTcmd;
/* FALLTHROUGH */
case 'i':
case K_INSERT:
ResetBuffers();
AppendToRedobuff("i");
doINSERTcmd:
CLEAROP;
n = RowNumber(Curschar);
AppendPositionToUndobuff(Curschar->index, n);
AppendPositionToUndoUndobuff(Curschar->index, n);
AppendToUndoUndobuff("i");
startinsert(FALSE);
CHANGED;
break;
case 'o':
CLEAROP;
ResetBuffers();
n = RowNumber(Curschar);
AppendToRedobuff("o");
AppendPositionToUndobuff(Curschar->index, n);
AppendPositionToUndoUndobuff(Curschar->index, n);
AppendToUndoUndobuff("o");
if (opencmd(FORWARD, TRUE)) {
startinsert(TRUE);
CHANGED;
}
last_command = 'o';
break;
case 'O':
CLEAROP;
ResetBuffers();
n = RowNumber(Curschar);
AppendToRedobuff("O");
AppendPositionToUndobuff(Curschar->index, n);
AppendPositionToUndoUndobuff(Curschar->index, n);
AppendToUndoUndobuff("O");
if (opencmd(BACKWARD, TRUE)) {
startinsert(TRUE);
CHANGED;
}
last_command = 'O';
break;
case 'd':
if (operator == DELETE) /* handle 'dd' */
goto lineop;
if (Prenum != 0)
opnum = Prenum;
startop = *Curschar;
operator = DELETE;
break;
/*
* Some convenient abbreviations...
*/
case 'x':
if (Prenum)
stuffnumReadbuff(Prenum);
stuffReadbuff("dl");
break;
case 'X':
if (Prenum)
stuffnumReadbuff(Prenum);
stuffReadbuff("dh");
break;
case 'D':
stuffReadbuff("d$");
break;
case 'Y':
if (Prenum)
stuffnumReadbuff(Prenum);
stuffReadbuff("yy");
break;
case 'C':
stuffReadbuff("c$");
break;
case 'c':
if (operator == CHANGE) { /* handle 'cc' */
CLEAROP;
stuffReadbuff("0c$");
break;
}
if (Prenum != 0)
opnum = Prenum;
startop = *Curschar;
operator = CHANGE;
break;
case 'y':
if (operator == YANK) /* handle 'yy' */
goto lineop;
if (Prenum != 0)
opnum = Prenum;
startop = *Curschar;
operator = YANK;
break;
case 'p':
if (RedrawingDisabled) {
RedrawingDisabled = FALSE;
updateNextscreen();
break;
} else if (Yankbuffptr != NULL) {
doput(FORWARD);
if (ybcrossline) {
n = RowNumber(Curschar);
stuffnumReadbuff(n);
stuffReadbuff("G");
if (ybtype == MCHAR) {
stuffnumReadbuff(Curschar->index);
if (Curschar->index)
stuffReadbuff("l");
} else
stuffnumReadbuff(0);
}
stuffReadbuff("p");
RedrawingDisabled = TRUE;
} else
beep();
break;
case 'P':
if (RedrawingDisabled) {
RedrawingDisabled = FALSE;
updateNextscreen();
break;
} else if (Yankbuffptr != NULL) {
doput(BACKWARD);
n = RowNumber(Curschar);
stuffnumReadbuff(n);
stuffReadbuff("G");
if (ybtype == MCHAR) {
stuffnumReadbuff(Curschar->index);
if (Curschar->index)
stuffReadbuff("l");
} else
stuffnumReadbuff(0);
stuffReadbuff("P");
RedrawingDisabled = TRUE;
} else
beep();
break;
case '>':
if (operator == RSHIFT) /* handle >> */
goto lineop;
if (operator == LSHIFT) {
CLEAROP;
beep();
break;
}
if (Prenum != 0)
opnum = Prenum;
startop = *Curschar; /* save current position */
operator = RSHIFT;
break;
case '<':
if (operator == LSHIFT) /* handle << */
goto lineop;
if (operator == RSHIFT) {
CLEAROP;
beep();
break;
}
if (Prenum != 0)
opnum = Prenum;
startop = *Curschar; /* save current position */
operator = LSHIFT;
break;
case 's': /* substitute characters */
if (Prenum)
stuffnumReadbuff(Prenum);
stuffReadbuff("cl");
break;
case '?':
case '/':
case ':':
CLEAROP;
readcmdline(c, (char *) NULL);
break;
case 'n':
mtype = MCHAR;
mincl = FALSE;
set_want_col = TRUE;
repsearch(0);
break;
case 'N':
mtype = MCHAR;
mincl = FALSE;
set_want_col = TRUE;
repsearch(1);
break;
/*
* Character searches
*/
case 'T':
dir = BACKWARD;
/* FALLTHROUGH */
case 't':
type = 1;
goto docsearch;
case 'F':
dir = BACKWARD;
/* FALLTHROUGH */
case 'f':
docsearch:
mtype = MCHAR;
mincl = TRUE;
set_want_col = TRUE;
if ((nchar = vgetc()) == ESC) /* search char */
break;
if (!searchc(nchar, dir, type)) {
CLEAROP;
beep();
}
break;
case ',':
flag = 1;
/* FALLTHROUGH */
case ';':
mtype = MCHAR;
mincl = TRUE;
set_want_col = TRUE;
if (!crepsearch(flag)) {
CLEAROP;
beep();
}
break;
/*
* Function searches
*/
case '[':
dir = BACKWARD;
/* FALLTHROUGH */
case ']':
mtype = MLINE;
set_want_col = TRUE;
if (vgetc() != c)
beep();
if (!findfunc(dir)) {
CLEAROP;
beep();
}
break;
/*
* Marks
*/
case 'm':
CLEAROP;
if (!setmark(vgetc()))
beep();
break;
case '\'':
flag = TRUE;
/* FALLTHROUGH */
case '`':
{
LPTR mtmp, *mark = getmark(vgetc());
if (mark == NULL) {
CLEAROP;
beep();
} else {
mtmp = *mark;
setpcmark();
*Curschar = mtmp;
if (flag)
beginline(TRUE);
}
mtype = flag ? MLINE : MCHAR;
mincl = TRUE; /* ignored if not MCHAR */
set_want_col = TRUE;
}
break;
case 'r':
CLEAROP;
if (lineempty(Curschar)) { /* Nothing to replace */
beep();
break;
}
if ((nchar = vgetc()) == ESC)
break;
Prenum = DEFAULT1(Prenum);
n = strlen(Curschar->linep->s) - Curschar->index;
if (n < Prenum) {
beep();
break;
}
ResetBuffers();
nn = RowNumber(Curschar);
AppendPositionToUndobuff(Curschar->index, nn);
AppendPositionToUndoUndobuff(Curschar->index, nn);
while (Prenum > 0) {
AppendToRedobuff("r");
AppendToRedobuff(mkstr(nchar));
AppendToUndobuff("r");
AppendToUndobuff(mkstr(gchar(Curschar)));
AppendToUndoUndobuff("r");
AppendToUndoUndobuff(mkstr(nchar));
pchar(Curschar, nchar); /* Change current character. */
if (Prenum > 1) {
oneright();
AppendToRedobuff("l");
AppendToUndobuff("l");
AppendToUndoUndobuff("l");
}
Prenum--;
}
CHANGED;
updateline();
break;
case '~': /* swap case */
CLEAROP;
if (lineempty(Curschar)) {
beep();
break;
}
ResetBuffers();
n = RowNumber(Curschar);
AppendPositionToUndobuff(Curschar->index, n);
AppendPositionToUndoUndobuff(Curschar->index, n);
Prenum = DEFAULT1(Prenum);
if (Prenum > 0) {
AppendNumberToRedobuff(Prenum);
AppendNumberToUndobuff(Prenum);
AppendNumberToUndoUndobuff(Prenum);
}
AppendToRedobuff("~");
AppendToUndobuff("~");
AppendToUndoUndobuff("~");
while (Prenum > 0) {
c = gchar(Curschar);
if (isalpha(c)) {
if (islower(c))
pchar(Curschar, toupper(c));
else
pchar(Curschar, tolower(c));
}
if (!oneright())
break;
Prenum--;
}
CHANGED;
updateline();
break;
case UNDO_SHIFTJ:
CLEAROP;
if (UndoInProgress) {
if (dojoin(FALSE, FALSE)) {
CHANGED;
updateNextscreen();
}
break;
}
goto doSHIFTJcommand;
case 'J':
CLEAROP;
doSHIFTJcommand:
if (nextline(Curschar) == NULL) { /* on last line */
beep();
break;
}
ResetBuffers();
temp_Curschar = *Curschar;
nn = strlen(Curschar->linep->s);
if (nn < 0)
nn = 0;
n = RowNumber(&temp_Curschar);
AppendToRedobuff("J");
AppendPositionToUndobuff(nn, n);
AppendPositionToUndoUndobuff(0, n);
AppendToUndoUndobuff("J");
if (lineempty(nextline(Curschar))) {
AppendToUndobuff("a\n");
if (!dojoin(FALSE, TRUE)) {
beep();
break;
}
} else if (lineempty(Curschar)) {
AppendToUndobuff("i\n");
if (!dojoin(FALSE, TRUE)) {
beep();
break;
}
} else {
AppendToUndobuff("dli\n");
if (!dojoin(TRUE, TRUE)) {
beep();
break;
}
}
AppendToUndobuff(ESC_STR);
AppendPositionToUndobuff(nn, n);
CHANGED;
updateNextscreen();
break;
case K_CGRAVE: /* shorthand command */
CLEAROP;
stuffReadbuff(":e #\n");
break;
case 'Z': /* write, if changed, and exit */
if (vgetc() != 'Z') {
beep();
break;
}
if (Changed) {
if (Filename != NULL) {
if (!writeit(Filename, (LPTR *) NULL, (LPTR *) NULL))
return;
} else {
emsg("No output file");
return;
}
}
getout(0);
break;
case '.':
CLEAROP;
if (RedrawingDisabled) {
RedrawingDisabled = FALSE;
updateNextscreen();
} else if (Redobuffptr != NULL) {
stuffReadbuff(Redobuff);
stuffReadbuff(".");
RedrawingDisabled = TRUE;
} else
beep();
break;
case 'u':
case K_UNDO:
CLEAROP;
if (UndoInProgress) {
p = UndoUndobuff;
UndoUndobuff = Undobuff;
Undobuff = p;
p = UndoUndobuffptr;
UndoUndobuffptr = Undobuffptr;
Undobuffptr = p;
UndoInProgress = FALSE;
RedrawingDisabled = FALSE;
updateNextscreen();
} else if (Undobuffptr != NULL) {
stuffReadbuff(Undobuff);
stuffReadbuff("u");
UndoInProgress = TRUE;
RedrawingDisabled = TRUE;
} else {
beep();
}
break;
default:
CLEAROP;
beep();
break;
}
/*
* If an operation is pending, handle it...
*/
if (finish_op) { /* we just finished an operator */
if (operator == NOP) /* ... but it was cancelled */
return;
switch (operator) {
case LSHIFT:
case RSHIFT:
ResetBuffers();
n = RowNumber(&startop);
AppendPositionToUndobuff(startop.index, n);
AppendPositionToUndoUndobuff(startop.index, n);
if (Prenum != 0) {
AppendNumberToRedobuff(Prenum);
AppendNumberToUndobuff(Prenum);
AppendNumberToUndoUndobuff(Prenum);
}
AppendToRedobuff((operator == LSHIFT) ? "<" : ">");
AppendToUndobuff((operator == LSHIFT) ? ">" : "<");
AppendToUndoUndobuff((operator == LSHIFT) ? "<" : ">");
AppendToRedobuff(mkstr(c));
if (c == '>')
AppendToUndobuff("<");
else if (c == '<')
AppendToUndobuff(">");
else
AppendToUndobuff(mkstr(c));
AppendToUndoUndobuff(mkstr(c));
doshift(operator);
CHANGED;
break;
case DELETE:
ResetBuffers();
n = RowNumber(&startop);
AppendPositionToUndoUndobuff(startop.index, n);
temp_Curschar = (lt(&startop, Curschar) ? startop : *Curschar);
n = RowNumber(&temp_Curschar);
if (Prenum != 0) {
AppendNumberToRedobuff(Prenum);
AppendNumberToUndoUndobuff(Prenum);
}
AppendToRedobuff("d");
AppendToUndoUndobuff("d");
AppendToRedobuff(mkstr(c));
AppendToUndoUndobuff(mkstr(c));
if (nchar != NUL) {
AppendToRedobuff(mkstr(nchar));
AppendToUndoUndobuff(mkstr(nchar));
}
AppendPositionToUndobuff(temp_Curschar.index, n);
dodelete(TRUE, TRUE, TRUE);
AppendPositionToUndobuff(temp_Curschar.index, n);
CHANGED;
break;
case YANK:
ResetBuffers(); /* no redo/undo/(undo of undo) on yank... */
doyank();
if (!ybcrossline)
*Curschar = startop;
else if (lt(&startop, Curschar))
*Curschar = startop;
break;
case CHANGE:
ResetBuffers();
n = RowNumber(&startop);
AppendPositionToUndoUndobuff(startop.index, n);
temp_Curschar = (lt(&startop, Curschar) ? startop : *Curschar);
n = RowNumber(&temp_Curschar);
if (mtype == MLINE)
AppendPositionToUndobuff(0, n);
else
AppendPositionToUndobuff(temp_Curschar.index, n);
if (Prenum != 0) {
AppendNumberToRedobuff(Prenum);
AppendNumberToUndoUndobuff(Prenum);
}
AppendToRedobuff("c");
AppendToUndoUndobuff("c");
AppendToRedobuff(mkstr(c));
AppendToUndoUndobuff(mkstr(c));
if (nchar != NUL) {
AppendToRedobuff(mkstr(nchar));
AppendToUndoUndobuff(mkstr(nchar));
}
dochange();
CHANGED;
last_command = 'c';
break;
default:
beep();
}
operator = NOP;
}
}
/*
* tabinout(shift_type, num)
*
* If shift_type == RSHIFT, add a tab to the begining of the next num lines;
* otherwise delete a tab from the beginning of the next num lines.
*/
static void
tabinout(shift_type, num)
int shift_type;
int num;
{
LPTR *p;
beginline(FALSE);
while (num-- > 0) {
beginline(FALSE);
if (shift_type == RSHIFT)
inschar(TAB);
else {
if (gchar(Curschar) == TAB)
delchar(TRUE, FALSE);
}
if (num > 0) {
if ((p = nextline(Curschar)) != NULL)
*Curschar = *p;
else
break;
}
}
}
/*
* doshift - handle a shift operation
*/
static void
doshift(op)
int op;
{
LPTR top, bot;
int nlines;
top = startop;
bot = *Curschar;
if (lt(&bot, &top))
pswap(top, bot);
nlines = cntllines(&top, &bot);
*Curschar = top;
tabinout(op, nlines);
/*
* The cursor position afterward is the prior of the two positions.
*/
*Curschar = top;
/*
* If we were on the last char of a line that got shifted left, then move
* left one so we aren't beyond the end of the line
*/
if (gchar(Curschar) == NUL && Curschar->index > 0)
Curschar->index--;
if (op == RSHIFT)
oneright();
else
oneleft();
updateNextscreen();
if (nlines > P(P_RP))
smsg("%d lines %ced", nlines, (op == RSHIFT) ? '>' : '<');
}
/*
* dodelete - handle a delete operation
*/
static void
dodelete(redraw, setup_for_undo, try_to_yank)
bool_t redraw;
bool_t setup_for_undo;
bool_t try_to_yank;
{
LPTR top, bot;
int nlines;
int n;
/*
* Do a yank of whatever we're about to delete. If there's too much stuff
* to fit in the yank buffer, then get a confirmation before doing the
* delete. This is crude, but simple. And it avoids doing a delete of
* something we can't put back if we want.
*/
if (try_to_yank) {
if (!doyank()) {
msg("yank buffer exceeded: press <y> to confirm");
if (vgetc() != 'y') {
msg("delete aborted");
*Curschar = startop;
return;
}
}
}
top = startop;
bot = *Curschar;
if (lt(&bot, &top))
pswap(top, bot);
*Curschar = top;
nlines = cntllines(&top, &bot);
cursupdate();
if (mtype == MLINE) {
if (operator == CHANGE) {
last_command_char = 'a';
delline(nlines - 1);
Curschar->index = 0;
while (delchar(TRUE, FALSE));
} else {
if (bot.linep->next == Fileend->linep)
last_command_char = 'o';
else
last_command_char = 'O';
if (setup_for_undo)
AppendToUndobuff(mkstr(last_command_char));
delline(nlines);
}
} else if (top.linep == bot.linep) { /* del. within line */
if (!mincl)
dec(&bot);
if (endofline(&bot))
last_command_char = 'a';
else
last_command_char = 'i';
if (setup_for_undo)
AppendToUndobuff(mkstr(last_command_char));
n = bot.index - top.index + 1;
while (n--)
if (!delchar(TRUE, FALSE))
break;
} else { /* del. between lines */
if (endofline(&top)) {
if (nextline(&top)) {
if (lineempty(nextline(&top)))
last_command_char = 'a';
else
last_command_char = 'i';
} else {
last_command_char = 'a';
}
} else {
last_command_char = 'i';
}
if (setup_for_undo)
AppendToUndobuff(mkstr(last_command_char));
n = Curschar->index;
while (Curschar->index >= n)
if (!delchar(TRUE, FALSE))
break;
top = *Curschar;
*Curschar = *nextline(Curschar);
delline(nlines - 2);
Curschar->index = 0;
n = bot.index;
if (!mincl)
n--;
while (n-- >= 0)
if (!delchar(TRUE, FALSE))
break;
*Curschar = top;
dojoin(FALSE, FALSE);
}
if (mtype == MCHAR && nlines == 1 && redraw)
updateline();
else
updateNextscreen();
if (nlines > P(P_RP))
smsg("%d fewer lines", nlines);
if (setup_for_undo) {
AppendToUndobuff(Yankbuff);
AppendToUndobuff(ESC_STR);
}
}
/*
* dochange - handle a change operation
*/
static void
dochange()
{
bool_t doappend; /* true if we should do append, not insert */
doappend = lt(Curschar, &startop); /* needed for some compilers */
doappend = endofline(doappend ? &startop : Curschar);
dodelete(FALSE, FALSE, TRUE);
if (doappend && !lineempty(Curschar))
inc(Curschar);
startinsert(FALSE);
}
static bool_t
doyank()
{
LPTR top, bot;
char *ybend = &Yankbuff[YANKSIZE - 1];
int nlines;
Yankbuffptr = Yankbuff;
top = startop;
bot = *Curschar;
if (lt(&bot, &top))
pswap(top, bot);
nlines = cntllines(&top, &bot);
ybtype = mtype; /* set the yank buffer type */
ybcrossline = FALSE;
if (LINEOF(&top) != LINEOF(&bot))
ybcrossline = TRUE;
if (mtype == MLINE) {
ybcrossline = TRUE;
top.index = 0;
bot.index = strlen(bot.linep->s);
/*
* The following statement checks for the special case of yanking a
* blank line at the beginning of the file. If not handled right, we
* yank an extra char (a newline).
*/
if (dec(&bot) == -1) {
*Yankbuff = NUL;
Yankbuffptr = NULL;
return TRUE;
}
} else {
if (!mincl)
if (!equal(&top, &bot))
dec(&bot);
}
for (; ltoreq(&top, &bot); inc(&top)) {
*Yankbuffptr = (gchar(&top) != NUL) ? gchar(&top) : NL;
Yankbuffptr++;
if (Yankbuffptr >= ybend) {
*Yankbuffptr = NUL;
msg("yank too big for buffer");
ybtype = MBAD;
return FALSE;
}
}
*Yankbuffptr = NUL;
if (operator == YANK)
if (nlines > P(P_RP))
smsg("%d lines yanked", nlines);
return TRUE;
}
static void
doput(dir)
int dir;
{
bool_t type;
if (ybtype == MBAD) {
beep();
return;
}
type = (ybtype == MCHAR);
if (dir == FORWARD)
stuffReadbuff(type ? "a" : "o");
else
stuffReadbuff(type ? "i" : "O");
stuffReadbuff(Yankbuff);
stuffReadbuff(ESC_STR);
}
static void
startinsert(startln)
int startln; /* if set, insert at start of line */
{
*Insstart = *Curschar;
if (startln) {
Insstart->index = 0;
}
Insbuffptr = Insbuff;
*Insbuffptr = NUL;
State = INSERT;
if (P(P_MO))
msg("Insert Mode");
}
void
ResetBuffers()
{
if (UndoInProgress)
return;
*Redobuff = NUL;
Redobuffptr = NULL;
*Undobuff = NUL;
Undobuffptr = NULL;
*UndoUndobuff = NUL;
UndoUndobuffptr = NULL;
}
void
AppendToRedobuff(s)
char *s;
{
if (UndoInProgress)
return;
if (Redobuffptr == NULL) {
if ((strlen(s) + 1) < REDO_UNDO_SIZE) {
strcpy(Redobuff, s);
Redobuffptr = Redobuff;
} else
emsg("Couldn't AppendToRedobuff() - should never happen");
return;
}
if ((strlen(Redobuff) + strlen(s) + 1) < REDO_UNDO_SIZE) {
strcat(Redobuff, s);
return;
}
emsg("Couldn't AppendToRedobuff() - should never happen");
}
void
AppendNumberToRedobuff(n)
int n;
{
char buf[32];
if (UndoInProgress)
return;
sprintf(buf, "%d", n);
AppendToRedobuff(buf);
}
void
AppendToUndobuff(s)
char *s;
{
if (UndoInProgress)
return;
if (Undobuffptr == NULL) {
if ((strlen(s) + 1) < REDO_UNDO_SIZE) {
strcpy(Undobuff, s);
Undobuffptr = Undobuff;
} else
emsg("Couldn't AppendToUndobuff() - should never happen");
return;
}
if ((strlen(Undobuff) + strlen(s) + 1) < REDO_UNDO_SIZE) {
strcat(Undobuff, s);
return;
}
emsg("Couldn't AppendToUndobuff() - should never happen");
}
void
AppendNumberToUndobuff(n)
int n;
{
char buf[32];
if (UndoInProgress)
return;
sprintf(buf, "%d", n);
AppendToUndobuff(buf);
}
void
AppendPositionToUndobuff(column, row)
int column;
int row;
{
if (UndoInProgress)
return;
AppendNumberToUndobuff(row);
AppendToUndobuff("G");
AppendNumberToUndobuff(column);
if (column)
AppendToUndobuff("l");
}
void
AppendToUndoUndobuff(s)
char *s;
{
if (UndoInProgress)
return;
if (UndoUndobuffptr == NULL) {
if ((strlen(s) + 1) < REDO_UNDO_SIZE) {
strcpy(UndoUndobuff, s);
UndoUndobuffptr = Undobuff;
} else
emsg("Couldn't AppendToUndoUndobuff() - should never happen");
return;
}
if ((strlen(UndoUndobuff) + strlen(s) + 1) < REDO_UNDO_SIZE) {
strcat(UndoUndobuff, s);
return;
}
emsg("Couldn't AppendToUndoUndobuff() - should never happen");
}
void
AppendNumberToUndoUndobuff(n)
int n;
{
char buf[32];
if (UndoInProgress)
return;
sprintf(buf, "%d", n);
AppendToUndoUndobuff(buf);
}
void
AppendPositionToUndoUndobuff(column, row)
int column;
int row;
{
if (UndoInProgress)
return;
AppendNumberToUndoUndobuff(row);
AppendToUndoUndobuff("G");
AppendNumberToUndoUndobuff(column);
if (column)
AppendToUndoUndobuff("l");
}
static bool_t
dojoin(leading_space, strip_leading_spaces)
bool_t leading_space;
bool_t strip_leading_spaces;
{
int scol; /* save cursor column */
int currsize; /* size of the current line */
int nextsize; /* size of the next line */
if (nextline(Curschar) == NULL) /* on last line */
return FALSE;
if (!canincrease(nextsize = strlen(Curschar->linep->next->s)))
return FALSE;
currsize = strlen(Curschar->linep->s);
while (oneright()); /* to end of line */
strcat(Curschar->linep->s, Curschar->linep->next->s);
/*
* Delete the following line. To do this we move the cursor there
* briefly, and then move it back. Don't back up if the delete made us
* the last line.
*/
Curschar->linep = Curschar->linep->next;
scol = Curschar->index;
if (nextline(Curschar) != NULL) {
delline(1);
Curschar->linep = Curschar->linep->prev;
} else
delline(1);
Curschar->index = scol;
if (currsize)
oneright(); /* go to first char. of joined line */
if (nextsize != 0 && strip_leading_spaces) {
/*
* Delete leading white space on the joined line and insert a single
* space.
*/
while (gchar(Curschar) == ' ' || gchar(Curschar) == TAB) {
delchar(TRUE, TRUE);
}
if (leading_space)
inschar(' ');
}
return TRUE;
}
char *
mkstr(c)
char c;
{
static char s[2];
s[0] = c;
s[1] = NUL;
return s;
}